//
//  LGTeamDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/16.
//  团队详情

#import "LGTeamDetailController.h"
//成员详情
#import "LGPersionalDataController.h"
//团队成员
#import "LGTeamMemberController.h"
//修改团队名称、介绍
#import "LGTeamContentController.h"
#import "LGEditTeamContentController.h"

#import "LGTeamConsultSettingVC.h"
//成员
#import "LGTeamDetailMemberView.h"
//管理员设置
#import "LGTeamDetailSettingView.h"
//团队内容
#import "LGTeamDetailContentView.h"

#import "LGTeamMemberModel.h"

#define scrollHeight (Screen_H-topBarHeight-viewPix(90))

@interface LGTeamDetailController ()<LGTeamDetailMemberDelegate,LGTeamDetailContentDelegate>
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)LGTeamDetailMemberView *memberView;
/**   */
@property (nonatomic , strong)LGTeamDetailSettingView *settingView;
/**   */
@property (nonatomic , strong)LGTeamDetailContentView *contentView;
/**   */
@property (nonatomic , strong)UIButton *quitButton;
/**  是否队长 */
@property (nonatomic , copy)NSString *isLeader;
/**  是否管理员 */
@property (nonatomic , copy)NSString *isManage;
/**  是否加入到团队中 */
@property (nonatomic , copy)NSString *isInto;
/**   */
@property (nonatomic , strong)NSArray *memberArray;
/**   */
@property (nonatomic , strong)NSDictionary *infoDic;
@end

@implementation LGTeamDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"";
    [self createSubView];
    [self emptyViewWithSuperView:self.view  frame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight)];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}


-(void)requestData{
    [RequestUtil withPOST:@"/doctor/my.teams/detail" parameters:@{@"teamid":self.teamId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.infoDic = returnData[@"info"];
            self.roleType = LGNSString(returnData[@"role_type"]);
            self.memberArray = [LGTeamMemberModel mj_objectArrayWithKeyValuesArray:returnData[@"members"]];
        }else{
            [self.emptyView showViewWithImage:LGEmptyPicName content:message offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView showViewWithImage:LGEmptyPicName content:LGErrorMessage offset:0];
    }];
}

-(void)setMemberArray:(NSArray *)memberArray{
    _memberArray = memberArray;
    self.memberView.dataArray = memberArray;
    for (LGTeamMemberModel *model in memberArray) {
        if(model.ismanage == YES){
            self.settingView.manage = model.realname;
            return;
        }
    }
}

-(void)setRoleType:(NSString *)roleType{
//    if ([roleType isEqualToString:@"10"] && [self.infoDic[@"isinto"] integerValue] == 1) {
//        //医生&&加入团队-->才能编辑
//        self.memberView.canEdit = YES;
//        self.contentView.canEdit = YES;
//    }else{
//        //不可编辑
//        self.memberView.canEdit = NO;
//        self.contentView.canEdit = NO;
//    }
}

-(void)setInfoDic:(NSDictionary *)infoDic{
    _infoDic = infoDic;
    self.title = LGNSString(infoDic[@"title"]);
    if([infoDic[@"isleader"] integerValue] == 0 && [infoDic[@"isinto"] integerValue] == 1){
        //非队长&&已加入 -- >可退出团队
        self.quitButton.hidden = NO;
        self.scrollView.frame = CGRectMake(0, 0, Screen_W, scrollHeight);
    }else{
        self.quitButton.hidden = YES;
        self.scrollView.frame = CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight);
    }
    
    if([infoDic[@"isleader"] integerValue] == 1){
        //队长-->可以设置管理员
        
        self.settingView.hidden = NO;
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.settingView.mas_bottom).offset(viewPix(16));
        }];
    }else{
        self.settingView.hidden = YES;
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.settingView.mas_bottom).offset(-viewPix(82));
        }];
    }
    
    if([infoDic[@"isleader"] integerValue] == 1 || [infoDic[@"ismanage"] integerValue] == 1){
        self.memberView.canEdit = YES;
        self.contentView.canEdit = YES;
    }else{
        self.memberView.canEdit = NO;
        self.contentView.canEdit = NO;
    }
    self.contentView.dataDic = infoDic;
}



//退出团队
-(void)quitButtonAction{
    __weak typeof(self) weakSelf = self;
    NSString *content = [NSString stringWithFormat:@"确定退出 %@ 团队吗？",self.title];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:content];
    [attrStr addAttributes:@{NSFontAttributeName:LGFontWeight(16, UIFontWeightMedium),NSForegroundColorAttributeName:[UIColor colorWithString:@"#429BFF"]} range:NSMakeRange(5, self.title.length)];
    [LGAlertTool alertWithTitle:@"退出团队" attrbutContent:attrStr sureAction:^{
        [RequestUtil withPOST:@"/doctor/my.teams/outTeam" parameters:@{@"teamid":weakSelf.teamId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                //返回至“我的”页面
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:@"退出失败，请重试" offset:0];
        }];
    }];
}


//查看成员详情
-(void)checkMemberDetail:(LGTeamMemberModel *)model{
    LGPersionalDataController *controller = [[LGPersionalDataController alloc]init];
    controller.userId = model.userid;
    [self.navigationController pushViewController:controller animated:YES];
}

//add、delete  添加、删除成员
-(void)editMemberWithType:(NSString *)type{
    LGTeamMemberController *controller = [[LGTeamMemberController alloc]init];
    controller.type = type; //add、delete  添加、删除成员
    controller.teamId = self.teamId;
    controller.teamTitle = LGNSString(self.infoDic[@"title"]);
    controller.teamIntroduce = LGNSString(self.infoDic[@"desc"]);
    [self.navigationController pushViewController:controller animated:YES];
}
//设置管理员
-(void)settingManageMember{
    LGTeamMemberController *controller = [[LGTeamMemberController alloc]init];
    controller.type = @"manage";
    controller.teamId = self.teamId;
    controller.teamTitle = LGNSString(self.infoDic[@"title"]);
    controller.teamIntroduce = LGNSString(self.infoDic[@"desc"]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)editTeamContent:(NSString *)type content:(nonnull NSString *)content{
    if([type isEqualToString:@"consult"]){
        //咨询
        LGTeamConsultSettingVC *controller = [[LGTeamConsultSettingVC alloc]init];
        controller.dataDic = self.infoDic;
        controller.teamId = LGNSString(self.infoDic[@"teamid"]);
        controller.number = LGNSString(self.infoDic[@"servicenums"]);
        [self.navigationController pushViewController:controller animated:YES];
        
    }else{
        if(self.contentView.canEdit == YES){
            //修改名称、介绍
            LGEditTeamContentController *controller = [[LGEditTeamContentController alloc]init];
            controller.type = type;
            controller.content = content;
            controller.teamId = self.teamId;
            [self.navigationController pushViewController:controller animated:YES];
        }else if ([type isEqualToString:@"desc"]){
            //查看内容
            LGTeamContentController *controller = [[LGTeamContentController alloc]init];
            controller.content = content;
            [self.navigationController pushViewController:controller animated:YES];
        }
    }
}

//成员View高度
-(void)memberViewHeight:(CGFloat)height{
    [self.memberView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height));
    }];
    [self performSelector:@selector(regreshContentSize) withObject:nil afterDelay:0.4];
}

//团队内容高度
-(void)contentViewHeight:(CGFloat)height{
    NSLog(@">>??>>%f",height);
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height));
    }];
    [self performSelector:@selector(regreshContentSize) withObject:nil afterDelay:0.4];
}
-(void)regreshContentSize{
    self.scrollView.contentSize = CGSizeMake(Screen_W, kMaxY(self.contentView.frame)+viewPix(90));
}


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.memberView];
    [self.scrollView addSubview:self.settingView];
    [self.scrollView addSubview:self.contentView];
    [self.view addSubview:self.quitButton];
    [self.memberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView).offset(viewPix(16));
        make.left.equalTo(self.view).offset(viewPix(16));
        make.right.equalTo(self.view).offset(-viewPix(16));
        make.height.equalTo(@(viewPix(253)));
    }];
    [self.settingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.memberView.mas_bottom).offset(viewPix(16));
        make.left.right.equalTo(self.memberView);
        make.height.equalTo(@(viewPix(82)));
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.settingView.mas_bottom).offset(viewPix(16));
        make.left.right.equalTo(self.memberView);
        make.height.equalTo(@(viewPix(207)));
    }];
    
}
-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight)];
        _scrollView.contentSize = CGSizeMake(Screen_W, Screen_H-topBarHeight);
        _scrollView.backgroundColor = [UIColor clearColor];
    }
    return _scrollView;
}

-(LGTeamDetailMemberView *)memberView{
    if(!_memberView){
        _memberView = [[LGTeamDetailMemberView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(183))];
        [_memberView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _memberView.backgroundColor = [UIColor whiteColor];
        _memberView.delegate = self;
    }
    return _memberView;
}

-(LGTeamDetailSettingView *)settingView{
    if(!_settingView){
        _settingView = [[LGTeamDetailSettingView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.memberView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(82))];
        [_settingView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _settingView.backgroundColor = [UIColor whiteColor];
        [_settingView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(settingManageMember)]];
    }
    return _settingView;
}

-(LGTeamDetailContentView *)contentView{
    if(!_contentView){
        _contentView = [[LGTeamDetailContentView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.memberView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(207))];
        [_contentView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.delegate = self;
    }
    return _contentView;
}

-(UIButton *)quitButton{
    if(!_quitButton){
        _quitButton = [UIButton buttonWithTitle:@"退出团队" topMargin:scrollHeight+viewPix(5)];
        [_quitButton addTarget:self action:@selector(quitButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _quitButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
